package com.zfzs.post.modules.goods.service.impl;

import com.alibaba.fastjson.JSON;
import com.zfzs.post.model.vo.QueryShopInfo;
import com.zfzs.post.modules.goods.domain.GoodOrderSn;
import com.zfzs.post.modules.goods.repository.GoodOrderSnRepository;
import com.zfzs.post.modules.goods.service.GoodOrderSnService;
import com.zfzs.post.modules.goods.service.dto.GoodOrderSnQueryCriteria;
import com.zfzs.post.modules.goods.service.mapper.GoodOrderSnMapper;
import com.zfzs.post.utils.PageUtil;
import com.zfzs.post.utils.QueryHelp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

/**
 * @author ck
 * @date 2019-10-18
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class GoodOrderSnServiceImpl implements GoodOrderSnService {

    @Autowired
    private GoodOrderSnRepository goodOrderSnRepository;

    @Autowired
    private GoodOrderSnMapper goodOrderSnMapper;

    @Override
    public Object queryAll(GoodOrderSnQueryCriteria criteria, Pageable pageable) {
        Page<GoodOrderSn> page = goodOrderSnRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        return PageUtil.toPage(page.map(goodOrderSnMapper::toDto));
    }

    @Override
    public Object queryAll(GoodOrderSnQueryCriteria criteria) {
        return goodOrderSnMapper.toDto(goodOrderSnRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
    }

    @Override
    public GoodOrderSn findById(Integer id) {
        Optional<GoodOrderSn> goodOrderSn = goodOrderSnRepository.findById(id);
        if (!goodOrderSn.isPresent()) {
            return null;
        }
        return goodOrderSn.get();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public GoodOrderSn create(GoodOrderSn resources) {
        resources.setCreateDate(Timestamp.valueOf(LocalDateTime.now()));
        resources.setCreateTime(Timestamp.valueOf(LocalDateTime.now()));
        return goodOrderSnRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(GoodOrderSn resources) {
        Optional<GoodOrderSn> optionalGoodOrderSn = goodOrderSnRepository.findById(resources.getOrderSnId());
        GoodOrderSn goodOrderSn = optionalGoodOrderSn.get();
        goodOrderSn.copy(resources);
        goodOrderSn.setUpdateTime(Timestamp.valueOf(LocalDateTime.now()));
        goodOrderSnRepository.save(goodOrderSn);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer id) {
        goodOrderSnRepository.deleteById(id);
    }

    @Override
    public List<QueryShopInfo> querySntByorderNoList(String orderNo, Long channelId) {
        List<Map<String, Object>> list = goodOrderSnRepository.querySntByorderNoList(orderNo,channelId);
        List<QueryShopInfo> collect = list.stream()
                .map(e -> {
                    QueryShopInfo good = JSON.parseObject(JSON.toJSONString(e), QueryShopInfo.class);
                    return good;
                }).collect(Collectors.toList());
        return collect;
    }
}